package forward.Module.Modules.Combat;

import forward.api.EventHandler;
import forward.api.Events.World.EventPacketReceive;
import forward.api.Events.World.EventPreUpdate;
import forward.Module.Module;
import forward.Module.ModuleType;
import forward.Util.TimerUtil;
import java.awt.Color;

import net.minecraft.item.ItemBow;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
import net.minecraft.network.play.server.S18PacketEntityTeleport;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;

public class FastBow extends Module {
	int counter;
	private TimerUtil timer = new TimerUtil();

	public FastBow() {
		super("FastBow", new String[] { "zoombow", "quickbow" }, ModuleType.Combat);
		this.setColor(new Color(255, 99, 99).getRGB());
		this.counter = 0;
	}

	private boolean canConsume() {
		if (this.mc.thePlayer.inventory.getCurrentItem() != null
				&& this.mc.thePlayer.getCurrentEquippedItem().getItem() instanceof ItemBow) {
			return true;
		}
		return false;
	}

	private void killGuardian() {
		if (this.timer.hasReached(1000.0)) {
			this.mc.thePlayer.sendQueue
					.addToSendQueue(new C03PacketPlayer.C04PacketPlayerPosition(this.mc.thePlayer.posX,
							this.mc.thePlayer.posY - Double.POSITIVE_INFINITY, this.mc.thePlayer.posZ, false));
			this.timer.reset();
		}
	}

	@EventHandler
	private void onUpdate(EventPreUpdate e) {
		if (this.mc.thePlayer.onGround && this.canConsume() && this.mc.gameSettings.keyBindUseItem.pressed) {
			this.mc.thePlayer.sendQueue
					.addToSendQueue(new C08PacketPlayerBlockPlacement(this.mc.thePlayer.inventory.getCurrentItem()));
			int i = 0;
			while (i < 20) {
				this.mc.thePlayer.sendQueue.addToSendQueue(new C03PacketPlayer.C04PacketPlayerPosition(
						this.mc.thePlayer.posX, this.mc.thePlayer.posY + 1.0E-9, this.mc.thePlayer.posZ, true));
				++i;
			}
			this.mc.thePlayer.sendQueue.addToSendQueue(new C07PacketPlayerDigging(
					C07PacketPlayerDigging.Action.RELEASE_USE_ITEM, BlockPos.ORIGIN, EnumFacing.DOWN));
		} else {
			this.mc.timer.timerSpeed = 1.0f;
		}
	}

	@EventHandler
	public void onRecieve(EventPacketReceive event) {
		if (event.getPacket() instanceof S18PacketEntityTeleport) {
			S18PacketEntityTeleport packet = (S18PacketEntityTeleport) event.getPacket();
			if (this.mc.thePlayer != null) {
				packet.yaw = (byte) mc.thePlayer.rotationYaw;
			}
			packet.pitch = (byte) mc.thePlayer.rotationPitch;
		}
	}
}
